import { createRouter, createWebHistory } from "vue-router";
// import HomeView from "../views/HomeView.vue";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/login",
            name: "login",
            component: () => import("../views/LoginView.vue"),
        },
        {
            path: "/",
            name: "layout",
            component: () => import("../views/LayoutView.vue"),
            // 孩子路由
            children: [
                {
                    path: "/home",
                    name: "home",
                    component: () => import("../views/HomeView.vue"),
                },
                {
                    path: "blog/:id?",
                    name: "blog",
                    component: () => import("../views/BlogView.vue"),
                },
                {
                    path: "about",
                    name: "about",
                    component: () => import("../views/AboutView.vue"),
                },
                {
                    path: "contact",
                    name: "contact",
                    component: () => import("../views/ContactView.vue"),
                },
            ],
        },
    ],
});

// 全局导航守卫
router.beforeEach((to, from, next) => {
    const isLogin = localStorage.getItem("isLogin");
    if (isLogin) {
        return next();
    } else {
        if (to.path != "/login") {
            return next("/login");
        } else {
            next();
        }
    }
});
export default router;
